//
//  YLCollectionCalculatorController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/15.
//  Copyright © 2017年 she1323. All rights reserved.
//  收款计算器

#import "YLCollectionCalculatorController.h"
#import "YLCollectionCalculatorHeader.h"
#import "YLGlobeHeader.h"
#import "YLSubTitileNoArrowTableViewCell.h"
#import "YLCollectionCalculatorViewModel.h"
#import "YLTitleHeader.h"
#import "YLProductModel.h"
@interface YLCollectionCalculatorController ()
@property (nonatomic, strong) YLCollectionCalculatorViewModel *viewModel;
@end

@implementation YLCollectionCalculatorController

- (YLCollectionCalculatorViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLCollectionCalculatorViewModel alloc] init];
        _viewModel.model = self.model;
    }
    return _viewModel;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"收款计算器";
   
    YLCollectionCalculatorHeader *header = [[YLCollectionCalculatorHeader alloc] init];
    header.frame = CGRectMake(0, 0, YLScreenW, YLScreenW / 750.0 * 130);
    
    __weak typeof(self) weakSelf = self;
    
    self.tableView.tableHeaderView = header;
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLSubTitileNoArrowTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLSubTitileNoArrowTableViewCell cellID]];
    
    header.moneyChangeBlock = ^(NSInteger money) {
        weakSelf.viewModel.money = money;
        [weakSelf.tableView reloadData];
    };
    
    header.leastMoney = _model.leastMoney;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (instancetype)init
{
    return [self initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    YLCollectionCalculatorGroup *group = self.viewModel.groups[section];
    return group.models.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YLCollectionCalculatorGroup *group = self.viewModel.groups[indexPath.section];

    YLSubTitileNoArrowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLSubTitileNoArrowTableViewCell cellID] forIndexPath:indexPath];
    cell.model = group.models[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    YLCollectionCalculatorGroup *group = self.viewModel.groups[section];
    if (group.models.count == 0) {
        return 0.0001;
    }
    
    return 10;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    YLCollectionCalculatorGroup *group = self.viewModel.groups[section];
    if (group.title == nil) {
        return 10;
    }

    return 60;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    YLCollectionCalculatorGroup *group = self.viewModel.groups[section];
    
    if (group.title == nil) {
        return [UIView new];
    }
    
    
    YLTitleHeader *header = [YLTitleHeader new];
    header.title = group.title;
    
    if (group.models.count == 0) {
        header.titleColor = YLMainColor;
        header.hiddenLine = NO;
    }
    return header;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 50;
}

@end
